package com.foryou.wechat.utils;

import com.foryou.wechat.common.Constants;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class HttpContextUtils {

	public static HttpServletRequest getHttpServletRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}

	public static Long getUserId() {
		return (Long) getHttpServletRequest().getSession().getAttribute(Constants.SESSION_USER_KEY);
	}

	public static void setUserId(Long userId) {
		if (getUserId()!=null && userId.equals(getUserId())) {
			return;
		}
		getHttpServletRequest().getSession().setAttribute(Constants.SESSION_USER_KEY,userId);
	}
}
